import streamlit as st
import pandas as pd

with st.container():

    # key
    key = st.text_input("point name","like Ua")


    datatype = st.selectbox(
        "data type:",
        ("u16","u32","i16","i32","enum16","datetime"),
    )
    if datatype == "enum16":
        # 模拟学生成绩数据
        student_grades = pd.DataFrame(
            {
                "Real Value": [],
                "Cloud Value": [],
            }
        )
        # 定义列配置
        column_config = {
            "Real Value": {"label": "Real Value"},
            "Cloud Value": st.column_config.NumberColumn(
                label="Cloud Value",
                min_value=0,
                max_value=150,
                format="%d",
            ),
        }
        # 使用st.data_editor展示和编辑学生成绩，并应用列配置
        st.title("MAP")
        edited_grades = st.data_editor(
            student_grades,
            column_config=column_config,
            num_rows="dynamic",
        )
        # 显示编辑后的数据（用于确认更改）
        st.write("编辑后的MAP:")
        st.dataframe(edited_grades)


    regaddr = st.text_input("register address","like 0x0001")


    regcnt = st.number_input("register count:",min_value=1,max_value=2,step=1,value=1,format="%d")


    opt = ["***0x03***","***0x10***","***0x06***",":rainbow[0x03 and 0x10]",":rainbow[0x03 and 0x06]"]
    funcode = st.radio(
        "Function Code",
        opt,
        index=0
    )
    index = opt.index(funcode)
    if index == 0:
        read_code = "03"
        write_code = "0"
    elif index == 1:
        read_code = "0"
        write_code = "10"
    elif index == 2:
        read_code = "0"
        write_code = "06"
    elif index == 3:
        read_code = "03"
        write_code = "10"
    elif index == 4:
        read_code = "03"
        write_code = "06"


    unit = st.selectbox(
        "unit:",
        ("1000","100","10","1","0.1","0.01","0.001","0.000001"),
        index=5
    )
    st.write("You unit",unit)




    if datatype == "enum16":
        ####
        start_bit = st.number_input("start_bit",min_value=0,max_value=15,step=1,value=1,format="%d")
        st.write("The start bit is",start_bit)
        bit_len = st.number_input("bit_len",min_value=0,max_value=15,step=1,value=1,format="%d")
        st.write("The bit len is",bit_len)



    timeout = st.number_input("Modbus frame timeout:",min_value=300,max_value=3000,step=50,value=300,format="%d")
    st.write("The timeout is",timeout)



    point_interval = st.number_input("Modbus point interval:",min_value=10,max_value=500,step=10,value=20,format="%d")
    st.write("The point interval is",point_interval)




    shadow_flow_rank = st.selectbox(
        "point data rank:",
        ("1","2","3","4"),
    )
    if shadow_flow_rank != "rank 4":
        with st.form("percent"):
            shadow_flow_percent = st.slider("Form slider",min_value=0,max_value=100,value=5,step=5,format="%d")
            submitted = st.form_submit_button("Submit")
            if submitted:
                st.write("slider",shadow_flow_percent)



    shadow_flow_threshold = st.number_input("Modbus shadow_flow_threshold:",min_value=10,max_value=5000,step=50,value=25,format="%d")
    st.write("The shadow_flow_threshold is",shadow_flow_threshold)

    agree = st.checkbox("block read point")
    if agree:
        st.write("It's a block read")
        shadow_flow_read_mode = 1

    submitted = st.button("Submit")
    if submitted:
        result_string = f"- {key};{datatype};{regaddr};{regcnt};[{read_code},{write_code}];{unit};{timeout};{point_interval};{shadow_flow_rank};{shadow_flow_percent};{shadow_flow_threshold};1;;"
        st.write(result_string)